package de.playmee.views;

import java.util.ArrayList;
import java.util.List;

import de.playmee.ImageManager;
import de.playmee.Playlist;
import de.playmee.Playmee;
import de.playmee.Track;
import de.playmee.handler.MusicHandler;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class CoverFlowAdapter extends BaseAdapter {

	private Context context;
	private MusicHandler mHandler;
	private List<Long> trackIds = new ArrayList<Long>();
	private int albumArtSize;
	private ImageManager imageManager;

	public CoverFlowAdapter(Activity activity, MusicHandler mHandler, Playlist playlist,
			int albumArtSize) {
		this.mHandler = mHandler;
		context = activity;
		this.albumArtSize = albumArtSize;
		imageManager = ((Playmee) activity.getApplication()).getImageManager();

		if (playlist != null) {
			trackIds.addAll(playlist.getTrackIds());
		}
	}
	
	@Override
	public int getCount() {
		return trackIds.size();
	}
	
	@Override
	public Object getItem(int position) {
		return null; // not needed
	}
	
	@Override
	public long getItemId(int position) {
		return trackIds.get(position);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parnet) {
		ImageView view;
		if (convertView == null) {
			view = new ImageView(context);
			view.setLayoutParams(new CoverFlow.LayoutParams(albumArtSize, albumArtSize));
			view.setImageResource(Track.DUMMY_ALBUM_ART);
		} else {
			view = (ImageView) convertView;
		}
		
		long trackId = getItemId(position);
		Track track = mHandler.loadTrackWithHelpOfDatabaseId(trackId, false);
		
		synchronized(imageManager) {
			imageManager.displayImage(track, view);
		}
		
		return view;
	}
}